$(function () {
    checkRole();
})

// 检查是否是组长
function checkRole() {
    let data ={userCode:sessionStorage.getItem("userCode") }
    let result = jsonGetValue("/user/findUserByUserCode", data);
    console.log(result.data);
   if (result.data.roleCode != "leader"){
       alert("你不是组长，没有权限签到");
       $("#signHtml").html("<h1>你没有权限进行操作！</h1>");
   } else {
       getUserListByTeam(result.data.teamCode);
   }
}

// 检查今天是否签到
function checkSign(){
    let myDate = new Date();
    let data = {signTime:myDate.toLocaleDateString(),teamCode:$("#teamCode").text()}
    console.log(data)
    let result = jsonPost("/sign/checkSign",data);
    if (result.data == "success"){
        alert("你今天已经签过到，不能重复签到");
        $(".btn_sign").hide();
        $(".modalBox").hide();
    }
}

// 查询所有小组成员
function getUserListByTeam(teamCode) {
    let data = {teamCode:teamCode};
    let result = jsonGetValue("/user/getUserByTeamCode", data);
    console.log(result);
    setAllData(result.data);
}
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>" + data[i].id + '</td>\n' +
            "        <td>" + data[i].userCode + '</td>\n' +
            "        <td>" + data[i].name  + "</td>\n" +
            "        <td>" + data[i].phone + "</td>\n" +
            "        <td>" + data[i].urgentPeople + "</td>\n" +
            "        <td>" + data[i].createTime + "</td>\n" +
            "        <td>" + data[i].updateTime + "</td>\n";
    }
    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    getUserSign();
}

// 获取小组所有成员签到
function getUserSign() {
    let teamCode = $("#teamCode").text();
    let data = {teamCode:teamCode};
    console.log(data)
    let result = jsonGetValue("/user/getUserByTeamCode", data);
    console.log(result);
    setUserSignData(result);
}
// 给小组列表赋值
function setUserSignData(result) {
    let html = "";
    for (let i=0; i<result.data.length;i++) {
        html += "<tr>\n" +
            "        <td>" + result.data[i].userCode + '</td>\n' +
            "        <td>" + result.data[i].name  + "</td>\n" +
            "        <td>"
        for (let j=0;j<result.code.length;j++){
            html += '<label><input type="radio" id="'+ result.data[i].id + '" name="signState'+ result.data[i].id +'"  value="'+ result.code[j].code +'">'+ result.code[j].key +'</label>';
        }
       html += "</td>\n" +
        "       <td><input type='datetime-local' id='lateTime" + result.data[i].id + "' hidden='hidden'></td>";
    }
    $("#tbodyModalBox").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    checkLateTimeState();
}
// 显示迟到时间输入框
function checkLateTimeState() {
    let signState = $("input:radio[name^='signState']");
    $.each(signState,(i,state)=>{
        state.onchange=(e)=>{
            console.log(e)
            if (e.target.value == 'late' && e.target.checked){
                $("#lateTime" + e.target.id).show();
            } else if (e.target.value != 'late' && e.target.checked){
                $("#lateTime" + e.target.id).hide();
            }
        }
    })
}

// 签到记录添加
function addSign() {
    let data = getSignTeamUser();
    console.log(data);
    let result = jsonPost("/sign/addSign", data);
    console.log(result);
    if (result.data == "success"){
        alert("签到成功")
        $(".right").load("/html/sign/sign-info.html");
    }
}

// 获取签到模态框中的所有数据
function getSignTeamUser() {
    let data ={teamCode:$("#teamCode").text()}
    let result = jsonGetValue("/user/getUserByTeamCode",data);
    let teamUsers = [];
    let myDate = new Date();
    for (let i = 0; i < result.data.length; i++){
        userCode = result.data[i].userCode;
        name = result.data[i].name;
        teamCode = result.data[i].teamCode;
        signState = $("input[name='signState" + result.data[i].id + "']:checked").val();
        lateTime = $("#lateTime" + result.data[i].id + "").val();
        signTime = myDate.toLocaleDateString();
        let param = {
            userCode:userCode,
            name:name,
            teamCode:teamCode,
            signState:signState,
            lateTime:lateTime,
            signTime:signTime
        }
        teamUsers.push(param);
    }

    return teamUsers;
}


